package com.jaydenxiao.common.commonutils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import java.lang.ref.WeakReference;

/**
 * Created by hasee on 2017/10/26.
 */

public class DialogUtils {

    private static ProgressDialog progressDialog;
    private static WeakReference mWeakReference;

    public static void hintDialog(Activity context, String content) {
        AlertDialog dialog=new AlertDialog.Builder(context)
                .setTitle("提示")
                .setMessage(content)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }



    public static void showProgressDialog(Activity activity, String title){
        if(!isLiving(activity)){
            return;
        }
        if(mWeakReference == null){
            mWeakReference = new WeakReference(activity);
        }
        activity = (Activity) mWeakReference.get();
        if (progressDialog == null) {
            if (activity.getParent() != null) {
                progressDialog = new ProgressDialog(activity.getParent());
            } else {
                progressDialog = new ProgressDialog(activity);
            }
        }
        if (!progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog.setTitle(title);
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        } else {
            progressDialog.setTitle(title);
        }

    }




    /**
     * 判断activity是否存活
     * */
    private static boolean isLiving(Activity activity){
        if(activity == null){
            Log.d("wisely","activity == null");
            return false;
        }

        if (activity.isFinishing()){
            return false;
        }
        return true;
    }


    public static void dismissProgressDialog(){
        if (isShowing(progressDialog)&& isExist_Living(mWeakReference))  {
            progressDialog.dismiss();
            progressDialog = null;
            mWeakReference.clear();
            mWeakReference = null;
        }

    }




    public static void closeProgressDialog(boolean flag){
        if (isShowing(progressDialog) && flag) {
            progressDialog.dismiss();
            progressDialog = null;
            mWeakReference.clear();
            mWeakReference = null;
        }
    }

    private static boolean isExist_Living(WeakReference<Activity> weakReference) {

        if(weakReference != null){
            Activity activity = weakReference.get();
            if (activity == null) {
                return false;
            }
            if (activity.isFinishing()) {
                return false;
            }
            return true;
        }
        return false;
    }


    /**
     * 判断进度框是否正在显示
     */
    private static boolean isShowing(ProgressDialog dialog) {
        boolean isShowing = dialog != null
                && dialog.isShowing();
        Log.d("wisely",">------isShow:"+isShowing);
        return isShowing;
    }


}
